/**
 * Created by sunkejun on 2015/10/15.
 */

$(document).ready(function(){
    var table = new ComDataTable("/admin/ads/page",$('#dataTable'),"admin_ads");
    var renders = {
        id:admin_ads_list.editRender,
        state:admin_ads_list.stateRender,
    };
    table.setRenders(renders);
    table.init();
    admin_ads_list.$table=table;
    //<span class="label label-primary">Enable</span>


})
var admin_ads_list  = (function ($) {
    this.editRender = function( data, type, full, meta ) {
        return   '<button class="btn btn-sm btn-success"      onclick="admin_ads_list.view('+data+')">  <span class="fa fa-eye"></span></button>'+
            '&nbsp; <button class="btn btn-sm btn-warning"      onclick="admin_ads_list.editTarget('+data+')">  <span class="fa fa-edit"></span></button>'+
            '&nbsp; <button class="btn btn-sm btn-danger"      onclick="admin_ads_list.delete('+data+')">  <span class="fa fa-trash"></span></button>';
    }
    this.stateRender = function( data, type, full, meta ) {
        return '<div   class="switch"  > <input type="checkbox"  /> </div>';
    }



    this.delete = function(id){
        swal({
            title: language.getNorlanguage().delete_title,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: language.getNorlanguage().delete_sub,
            cancelButtonText: language.getNorlanguage().delete_cancle,
            closeOnConfirm: true,
            html: false
        }, function(){
            ComdataUtil.ajaxpostnormal('/admin/ads/'+id,{},function(data){
                this.$table.rdraw();
            },"DELETE");
        });
    }


    this.submit = function(){
        $('#com_message').html("");
        var data = ComdataUtil.collectData("actionmodifyform");
        ComdataUtil.ajaxpost('/admin/ads',data,this.finishAjaxLoadBack);
    }

    this.openAdd = function(){
        openModel(0);

        $('.switch').bootstrapSwitch();
    }


    this.addNew = function(){
        openModel(0);
    }
    this.getItem = function(item){
        //openModel();
        ComdataUtil.loadContainer("actionmodifyform",item);
    }
    this.editTarget = function(id){
        openModel();
        ComdataUtil.ajaxpostnormal('/admin/ads/'+id,{},this.getItem,"GET");

    }

    this.openModel = function(type) {
        ComdataUtil.clear("actionmodifyform");
        $("#ads_modal").modal('show');
    }
    this.closeModel = function() {
        $("#ads_modal").modal('hide');
    }


    this.submit = function(){
        $('#com_message').html("");
        var data = ComdataUtil.collectData("actionmodifyform");
        ComdataUtil.ajaxpost('/admin/ads',data,this.finishAjaxLoadBack);
    }

    this.view = function(id){
        $("#ad_prview").modal('show');
        $("#view_container").html("");
        ComdataUtil.ajaxpostnormal('/admin/ads/'+id,{},function(data){
            $("#view_container").html(data.ad_content);
        },"GET");
    }
    this.finishAjaxLoadBack = function(response){
        if(response.code == 500){
            openToastr.open(3,response.res_data);
            //$('#com_message').attr("class","text-danger text-center");
            //$('#com_message').html(response.res_data);
        }else{
            ComdataUtil.clear("actionmodifyform");
            closeModel();
            this.$table.rdraw();
        }

    }
    return this;
})(jQuery);

